package com.xy.tianyu.module.picture.fragment

import androidx.lifecycle.rxLifeScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.xy.tianyu.LazyLoadBaseFragment
import com.xy.tianyu.R
import com.xy.tianyu.module.picture.activity.PicHotDayDetailActivity
import com.xy.tianyu.module.picture.adapter.PicTodayHotAdapter
import com.xy.tianyu.module.picture.bean.Item
import com.xy.tianyu.module.picture.bean.PicTodayHotBean
import com.xy.tianyu.module.picture.bean.PicTodayHotMultipleItem
import com.xy.tianyu.network.Api
import kotlinx.android.synthetic.main.fragment_pic_today_hot.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import rxhttp.RxHttp
import rxhttp.async
import rxhttp.toClass

/**
 * @time 2020/9/30 9:46
 * @class 描述:
 */
class PicTodayHotFragment : LazyLoadBaseFragment() {
    private var picTodayHotList: MutableList<PicTodayHotMultipleItem> = mutableListOf()
    private lateinit var picTodayHotAdapter: PicTodayHotAdapter
    private var pageIndex = 0
    private val category = mutableListOf(
        "音乐", "靓女", "运动", "读书", "设计", "花卉", "舞蹈", "美食", "绘画", "科技", "游戏", "清新", "汽车", "星座", "时尚",
        "旅行", "教育", "搞笑", "情感", "影视", "帅哥", "家居", "动物", "动漫", "军事", "儿童", "健康", "文艺"
    )
    private val picVerList = mutableListOf<Item>()
    private val picSquareList = mutableListOf<Item>()

    override fun setContentView(): Int {
        return R.layout.fragment_pic_today_hot
    }

    override fun lazyLoad() {
        ry_pic_today_hot.layoutManager = LinearLayoutManager(activity)
        picTodayHotAdapter = PicTodayHotAdapter(picTodayHotList)
        ry_pic_today_hot.adapter = picTodayHotAdapter
        picTodayHotAdapter.addChildClickViewIds(R.id.iv_pic_today_hot_hor,R.id.iv_pic_today_hot_ver_one,R.id.iv_pic_today_hot_ver_two,
            R.id.iv_pic_today_hot_square_one,R.id.iv_pic_today_hot_square_two)
        picTodayHotAdapter.setOnItemChildClickListener { _, view, position ->
            val list = arrayListOf<Item>()
            val picHotItem = picTodayHotList[position]
            var item : Item
            picTodayHotList.forEach {
                when (it.itemType) {
                    PicTodayHotMultipleItem.PIC_HOT_TODAY_HOR -> {
                        list.add(it.picTodayHotItem1)
                    }
                    else -> {
                        list.add(it.picTodayHotItem1)
                        list.add(it.picTodayHotItem2)
                    }
                }
            }

            when (view.id) {
                R.id.iv_pic_today_hot_hor -> {
                    item = picHotItem.picTodayHotItem1
                }
                R.id.iv_pic_today_hot_ver_one -> {
                    item = picHotItem.picTodayHotItem1
                }
                R.id.iv_pic_today_hot_ver_two -> {
                    item = picHotItem.picTodayHotItem2
                }
                R.id.iv_pic_today_hot_square_one -> {
                    item = picHotItem.picTodayHotItem1
                }
                else -> {
                    item = picHotItem.picTodayHotItem2
                }
            }

            PicHotDayDetailActivity.actionStart1(activity!!,0,list,item)
        }
        getData(true)

        refresh_pic_hot.setOnRefreshListener {
            pageIndex = 0
            getData(true)
        }

        refresh_pic_hot.setOnLoadMoreListener {
            pageIndex++
            getData(false)
        }
    }

    override fun stopLoad() {

    }

    private fun getData(isRefresh: Boolean) {

        rxLifeScope.launch({
            if (isRefresh) {
                picTodayHotList.clear()
                picTodayHotAdapter.notifyDataSetChanged()
            }
            for (i in 0..2) {
                //循环三次接口
                val asyncPic = getPicTodayHot(this)
                val picTodayHotBean = asyncPic.await()
                val list = picTodayHotBean.items
                list.forEach {
                    val thumbWidth = it.pic_attrs.thumb_width
                    val thumbHeight = it.pic_attrs.thumb_height
                    when {
                        thumbWidth > thumbHeight -> {
                            val multipleItem = PicTodayHotMultipleItem(
                                PicTodayHotMultipleItem.PIC_HOT_TODAY_HOR,
                                it,
                                list.random()
                            )
                            picTodayHotList.add(multipleItem)
                        }
                        thumbWidth < thumbHeight -> {
                            picVerList.add(it)
                            if (picVerList.size == 2) {
                                val multipleItem = PicTodayHotMultipleItem(
                                    PicTodayHotMultipleItem.PIC_HOT_TODAY_VER,
                                    picVerList[0],
                                    picVerList[1]
                                )
                                picTodayHotList.add(multipleItem)
                                picVerList.clear()
                            }
                        }
                        else -> {
                            picSquareList.add(it)
                            if (picSquareList.size == 2) {
                                val multipleItem = PicTodayHotMultipleItem(
                                    PicTodayHotMultipleItem.PIC_HOT_TODAY_SQUARE,
                                    picSquareList[0],
                                    picSquareList[1]
                                )
                                picTodayHotList.add(multipleItem)
                                picSquareList.clear()
                            }
                        }
                    }
                }
            }
            picTodayHotAdapter.notifyDataSetChanged()
        }, {
            ToastUtils.showShort(it.message)
        }, {

        }, {
            refresh_pic_hot.finishRefresh()
            refresh_pic_hot.finishLoadMore()
        })
    }

    private suspend fun getPicTodayHot(scope: CoroutineScope): Deferred<PicTodayHotBean> {

        return RxHttp.get(
            Api.DAY_DAY_SEARCH_PICTURE_BASE_URL
                    + "/pictures/${category.random()}/$pageIndex"
        )
            .toClass<PicTodayHotBean>()
            .async(scope)
    }


}